package com.rx.mgt.controller;

import com.rx.mgt.domain.AdminUser;
import com.rx.mgt.domain.UserInfoExt;
import com.rx.mgt.po.UsersBo;
import com.rx.mgt.service.DomainService;
import com.rx.mgt.service.SysLogService;
import com.rx.mgt.service.UserInfoExtService;
import com.rx.mgt.util.IpUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zxj on 2017/5/31.
 */
@Controller
public class EmailAliasController {

    @Autowired
    UserInfoExtService userInfoExtService;
    @Autowired
    DomainService domainService;
    @Autowired
    SysLogService sysLogService;
    @RequestMapping("/email-alias-list.htmlx")
    public String emailAliasList(HttpServletRequest request){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        UsersBo usersBo = new UsersBo();
        List list = domainService.getDomainInfoExt(usersBo);
        String type = user.getType();
        if("2".equals(type)){
            request.setAttribute("dom",user.getDomain());
        }
        request.setAttribute("domainList",list);
        return "email/email-alias-list";
    }

    /**
     * ajax 获取邮件别名列表
     */
    @RequestMapping("/ajaxGetEmailAliasList.htmlx")
    @ResponseBody
    public Map<String,Object> ajaxGetAliasEmailList(@RequestParam("domain")String domain){
        Map<String,Object> rs = new HashMap<String, Object>();
        UsersBo usersBo = new UsersBo();
        usersBo.setDomain(domain);
        List<UserInfoExt> list = userInfoExtService.getAllUserHasNikeName(usersBo);
        rs.put("tableData",list);
        return rs;
    }
    /**
     * 编辑别名页面
     */
    @RequestMapping("/email-alias-update.htmlx")
    public String updateEmailAlisa(@RequestParam("id")String id,Map<String,Object>rs){
        UserInfoExt ext = userInfoExtService.get(id);
        rs.put("userinfo",ext);
        return "email-alias-update";
    }
    /**
     * 修改别名
     */
    @RequestMapping("/emailAliasUpdate.htmlx")
    @ResponseBody
    public Map<String,Object> updateEmailAlias(UserInfoExt infoExt,HttpServletRequest request){
        Map rs= new HashMap();
        String s = userInfoExtService.updateEmailAlias(infoExt);
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        sysLogService.add("修改邮件别名", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());
        if(StringUtils.isNotBlank(s)){
            rs.put("code","001");
        }else{
            rs.put("code","002");
        }
        return rs;
    }

    /**
     * 删除邮件别名
     */
    @RequestMapping("/deleteEmailAlias.htmlx")
    @ResponseBody
    public Map<String,Object> deleteEmailAlias(@RequestParam("id")String id,HttpServletRequest request){
        Map<String,Object> rs = new HashMap<String, Object>();
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        int i = userInfoExtService.deleteEmailAlias(id);
        sysLogService.add("删除邮件别名", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());

        if(i>0){
            rs.put("code","001");
        }else{
            rs.put("code","002");
        }
        return  rs;
    }
    /**
     * 新增邮箱别名页面
     */
    @RequestMapping("/domain-alias-add.htmlx")
    public String emailAliasAdd(@RequestParam(value = "domain",required = false)String domain,HttpServletRequest request){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        if("2".equals(user.getType())){
            domain = user.getDomain();
        }
        request.setAttribute("domain","@"+domain);
        return  "domain-alias-add";
    }

    /**
     * 新增邮箱别名
     */
    @RequestMapping("/emailAliasSave.htmlx")
    @ResponseBody
    public Map<String,Object> emailAliasSave(HttpServletRequest request,UserInfoExt infoExt){
        Map<String,Object> rs = new HashMap<String, Object>();
        infoExt.setUser_name(infoExt.getUser_name()+infoExt.getDomain());
        UserInfoExt us= userInfoExtService.getUserInfoByNickName(infoExt.getNike_name());
        if(us!=null){
            rs.put("code","003");
            return rs;
        }
        Integer integer = userInfoExtService.emailAliasSave(infoExt);
        if(integer>0){
            rs.put("code","001");
        }else{
            rs.put("code","002");
        }

        return  rs;
    }
}
